package org.example.handler;

import org.example.rvo.ResultVO;
import org.example.util.JSONUtil;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Component
public class CustomerLoginOutHandler implements LogoutHandler {


    @Override
    public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.write(JSONUtil.obj2str(ResultVO.success("200","用户登出成功",((User)authentication.getPrincipal()).toString())));
            out.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            out.close();
        }
    }
}
